package com.hxzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.dto.GoodsInfoDTO;
import com.hxzy.dto.PageDTO;
import com.hxzy.entity.TbNewbeeMallAdminUser;
import com.hxzy.entity.TbNewbeeMallGoodsCategory;
import com.hxzy.entity.TbNewbeeMallGoodsInfo;
import com.hxzy.mapper.TbNewbeeMallGoodsCategoryMapper;
import com.hxzy.mapper.TbNewbeeMallGoodsInfoMapper;
import com.hxzy.service.TbNewbeeMallGoodsInfoService;
import com.hxzy.util.SessionUtil;
import com.hxzy.vo.BootstrapTableVO;
import com.hxzy.vo.GoodsInfoVO;
import com.hxzy.vo.ResultEnum;
import com.hxzy.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//开启事务自动提交
@Transactional
@Service
public class TbNewbeeMallGoodsInfoServiceImpl implements TbNewbeeMallGoodsInfoService {
    @Autowired
    private TbNewbeeMallGoodsInfoMapper tbNewbeeMallGoodsInfoMapper;

    @Autowired
    private TbNewbeeMallGoodsCategoryMapper tbNewbeeMallGoodsCategoryMapper;

    @Override
    public BootstrapTableVO search(GoodsInfoDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNumber(),pageDTO.getPageSize());
        List<TbNewbeeMallGoodsInfo> search = this.tbNewbeeMallGoodsInfoMapper.search(pageDTO);

        //自己装组，比数据库来得快
        List<GoodsInfoVO>  newGoodsArray=new ArrayList<>();

        for (TbNewbeeMallGoodsInfo g : search) {
            GoodsInfoVO goodsInfoVO=new GoodsInfoVO();
            //复制
            BeanUtils.copyProperties(g,goodsInfoVO);

            TbNewbeeMallGoodsCategory one = this.tbNewbeeMallGoodsCategoryMapper.selectByPrimaryKey(new Long(g.getGoodsOneCategoryId()));
            goodsInfoVO.setGoodsOneCategoryName(one.getCategoryName());

            TbNewbeeMallGoodsCategory two = this.tbNewbeeMallGoodsCategoryMapper.selectByPrimaryKey(new Long(g.getGoodsTwoCategoryId()));
            goodsInfoVO.setGoodsTwoCategoryName(two.getCategoryName());

            TbNewbeeMallGoodsCategory three = this.tbNewbeeMallGoodsCategoryMapper.selectByPrimaryKey(new Long(g.getGoodsThreeCategoryId()));
            goodsInfoVO.setGoodsThreeCategoryName(three.getCategoryName());

            newGoodsArray.add(goodsInfoVO);
        }
        BootstrapTableVO tableVO=new BootstrapTableVO();
        tableVO.setRows(newGoodsArray);
        tableVO.setTotal(new PageInfo(search).getTotal());
        return tableVO;
    }

    @Override
    public ResultVO updateByPrimaryKeySelective(TbNewbeeMallGoodsInfo record) {
        ResultVO resultVO;
        record.setUpdateTime(new Date());
        TbNewbeeMallAdminUser user = SessionUtil.getUser();
        record.setUpdateUser(user.getAdminUserId());

        int count=0;
        count=this.tbNewbeeMallGoodsInfoMapper.updateByPrimaryKeySelective(record);

        if (count>0){
            resultVO = ResultVO.success(ResultEnum.SUCDESS.getMessage());
        }else{
            resultVO=ResultVO.failed(ResultEnum.PARAMS_FAILED);
        }
        return resultVO;
    }

    @Override
    public ResultVO addOrUpdate(TbNewbeeMallGoodsInfo tbNewbeeMallGoodsInfo) {
        ResultVO resultVO=null;

        tbNewbeeMallGoodsInfo.setUpdateTime(new Date());
        TbNewbeeMallAdminUser user = SessionUtil.getUser();
        tbNewbeeMallGoodsInfo.setUpdateUser(user.getAdminUserId());

        int count=0;
        if (tbNewbeeMallGoodsInfo.getGoodsId()==null){
            tbNewbeeMallGoodsInfo.setCreateTime(new Date());
            tbNewbeeMallGoodsInfo.setCreateUser(user.getAdminUserId());
            tbNewbeeMallGoodsInfo.setGoodsCarousel(tbNewbeeMallGoodsInfo.getGoodsCoverImg());
            count=tbNewbeeMallGoodsInfoMapper.insert(tbNewbeeMallGoodsInfo);
        }else{
            count=tbNewbeeMallGoodsInfoMapper.updateByPrimaryKeySelective(tbNewbeeMallGoodsInfo);
        }


        if (count>0){
            resultVO = ResultVO.success(ResultEnum.SUCDESS.getMessage());
        }else{
            resultVO=ResultVO.failed(ResultEnum.DB_SAVE_FAILED);
        }
        return  resultVO;
    }

    @Override
    public TbNewbeeMallGoodsInfo selectByPrimaryKey(Long goodsId) {
        return this.tbNewbeeMallGoodsInfoMapper.selectByPrimaryKey(goodsId);
    }
}
